package com.example.qincetest.mvvm.first

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.qincetest.BaseActivity
import com.example.qincetest.network.OkHttpConfig
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import timber.log.Timber

class FirstActivity : BaseActivity() {

    val viewModel: FirstViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyScreen()
        }


//        lifecycleScope.launch {
//            viewModel.timeFlow.collect { time ->
//                Timber.d("flow-test----launch update UI $time")
//            }
//        }

//        lifecycleScope.launchWhenStarted {
//            viewModel.timeFlow.collect { time ->
//                Timber.d("flow-test----launchWhenStarted update UI $time")
//            }
//        }
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED) {
//                viewModel.timeFlow.collect { time ->
//                    Timber.d("flow-test----repeatOnLifecycle update UI $time")
//                }
//            }
//        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.hotFlow.collect { time ->
                    Timber.d("flow-test----repeatOnLifecycle update UI $time")
                }
            }
        }
    }

    @Composable
    fun MyScreen(
        modifier: Modifier = Modifier
    ) {
        val context = LocalContext.current

        Column(
            modifier = modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalAlignment = Alignment.Start
        ) {
            Button(
                onClick = {
                    lifecycleScope.launch {
                        val result = OkHttpConfig.api.getArticleList(0)
                        Timber.e(result.toString())
                    }
                }
            ) {
                Text("打开")
            }
            Button(
                onClick = {
                    lifecycleScope.launch {
                        val result = OkHttpConfig.api.getArticleList2(0)
                        Timber.e(result.toString())
                        Timber.e(result.body().toString())
                    }
                }
            ) {
                Text("打开2")
            }
            Button(
                onClick = {
                    val result = viewModel.getArticleList(0)
                    Timber.e(result.toString())
                }
            ) {
                Text("ViewModel打开")
            }

//            Button(
//                onClick = {
//                    lifecycleScope.launch {
//                        OkHttpConfig.api.getArticleList1(0).catch {
//                            Timber.e(it.toString())
//                        }.collect {
//                            Timber.e(it.toString())
//                        }
//                    }
//                }
//            ) {
//                Text("Flow打开")
//            }
        }
    }

    @Preview(
        name = "Light Mode",
        showBackground = true,
        backgroundColor = 0xFFFFFFFF
    )
    @Composable
    fun MainScreenPreview() {
        MyScreen()
    }
}